#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include "camera.h"
#include "../utils/log.h"

// 全局变量
static bool g_is_initialized = false;
static camera_config_t g_current_config;
static enum camera_exposure_mode g_exposure_mode = CAMERA_EXPOSURE_AUTO;
static float g_exposure_value = 0.0f;
static enum camera_white_balance g_white_balance = CAMERA_WB_AUTO;
static pthread_mutex_t g_camera_mutex = PTHREAD_MUTEX_INITIALIZER;

int camera_init(void)
{
    pthread_mutex_lock(&g_camera_mutex);
    
    if (g_is_initialized) {
        pthread_mutex_unlock(&g_camera_mutex);
        return CAMERA_SUCCESS;
    }
    
    // 初始化相机配置
    memset(&g_current_config, 0, sizeof(camera_config_t));
    g_current_config.resolution_width = 1920;
    g_current_config.resolution_height = 1080;
    g_current_config.format = CAMERA_FORMAT_JPEG;
    g_current_config.quality = 90;
    strncpy(g_current_config.save_path, "/tmp/photos", sizeof(g_current_config.save_path) - 1);
    
    LOG_INFO("相机模块初始化成功");
    g_is_initialized = true;
    
    pthread_mutex_unlock(&g_camera_mutex);
    return CAMERA_SUCCESS;
}

void camera_cleanup(void)
{
    pthread_mutex_lock(&g_camera_mutex);
    
    if (!g_is_initialized) {
        pthread_mutex_unlock(&g_camera_mutex);
        return;
    }
    
    LOG_INFO("相机模块清理完成");
    g_is_initialized = false;
    
    pthread_mutex_unlock(&g_camera_mutex);
}

int camera_capture_photo(camera_config_t *config, char *filename)
{
    char final_filename[256];
    time_t now;
    struct tm *tm_info;
    
    pthread_mutex_lock(&g_camera_mutex);
    
    if (!g_is_initialized) {
        pthread_mutex_unlock(&g_camera_mutex);
        LOG_ERROR("相机模块未初始化");
        return CAMERA_ERROR_INIT;
    }
    
    // 如果传入了配置参数，则更新配置
    if (config != NULL) {
        memcpy(&g_current_config, config, sizeof(camera_config_t));
    }
    
    // 生成文件名
    if (filename == NULL || strlen(filename) == 0) {
        time(&now);
        tm_info = localtime(&now);
        
        // 根据格式生成文件名
        const char *format_ext;
        switch (g_current_config.format) {
            case CAMERA_FORMAT_JPEG:
                format_ext = "jpg";
                break;
            case CAMERA_FORMAT_PNG:
                format_ext = "png";
                break;
            case CAMERA_FORMAT_RAW:
                format_ext = "raw";
                break;
            default:
                format_ext = "jpg";
        }
        
        snprintf(final_filename, sizeof(final_filename), "%s/drone_photo_%04d%02d%02d_%02d%02d%02d.%s",
                 g_current_config.save_path,
                 tm_info->tm_year + 1900,
                 tm_info->tm_mon + 1,
                 tm_info->tm_mday,
                 tm_info->tm_hour,
                 tm_info->tm_min,
                 tm_info->tm_sec,
                 format_ext);
    } else {
        snprintf(final_filename, sizeof(final_filename), "%s/%s",
                 g_current_config.save_path, filename);
    }
    
    // 模拟拍照过程
    LOG_INFO("拍摄照片，分辨率:%dx%d, 格式:%d, 质量:%d, 保存路径:%s",
             g_current_config.resolution_width,
             g_current_config.resolution_height,
             g_current_config.format,
             g_current_config.quality,
             final_filename);
    
    // 这里应该调用实际的相机拍照API
    // ...
    
    LOG_INFO("照片拍摄成功: %s", final_filename);
    
    pthread_mutex_unlock(&g_camera_mutex);
    return CAMERA_SUCCESS;
}

int camera_set_config(camera_config_t *config)
{
    pthread_mutex_lock(&g_camera_mutex);
    
    if (!g_is_initialized) {
        pthread_mutex_unlock(&g_camera_mutex);
        LOG_ERROR("相机模块未初始化");
        return CAMERA_ERROR_INIT;
    }
    
    if (config == NULL) {
        pthread_mutex_unlock(&g_camera_mutex);
        LOG_ERROR("配置参数为空");
        return CAMERA_ERROR_CONFIG;
    }
    
    memcpy(&g_current_config, config, sizeof(camera_config_t));
    LOG_INFO("相机配置已更新");
    
    pthread_mutex_unlock(&g_camera_mutex);
    return CAMERA_SUCCESS;
}

int camera_set_exposure_mode(enum camera_exposure_mode mode, float value)
{
    pthread_mutex_lock(&g_camera_mutex);
    
    if (!g_is_initialized) {
        pthread_mutex_unlock(&g_camera_mutex);
        LOG_ERROR("相机模块未初始化");
        return CAMERA_ERROR_INIT;
    }
    
    g_exposure_mode = mode;
    if (mode == CAMERA_EXPOSURE_MANUAL) {
        g_exposure_value = value;
        LOG_INFO("设置手动曝光模式，曝光值: %f", value);
    } else {
        LOG_INFO("设置曝光模式: %d", mode);
    }
    
    pthread_mutex_unlock(&g_camera_mutex);
    return CAMERA_SUCCESS;
}

int camera_set_white_balance(enum camera_white_balance wb)
{
    pthread_mutex_lock(&g_camera_mutex);
    
    if (!g_is_initialized) {
        pthread_mutex_unlock(&g_camera_mutex);
        LOG_ERROR("相机模块未初始化");
        return CAMERA_ERROR_INIT;
    }
    
    g_white_balance = wb;
    LOG_INFO("设置白平衡模式: %d", wb);
    
    pthread_mutex_unlock(&g_camera_mutex);
    return CAMERA_SUCCESS;
}

bool camera_is_available(void)
{
    bool ret;
    pthread_mutex_lock(&g_camera_mutex);
    ret = g_is_initialized;
    pthread_mutex_unlock(&g_camera_mutex);
    return ret;
}